import store from '../store'

export const getRequestId = () => Math.round(Math.random() * 1000000000000) + ''

export const S = (json) => {
  json = Object.assign({}, getSiteParams(true), json)
  return JSON.stringify(json)
}

export const U = (data) => {
  data = Object.assign({}, getSiteParams(true), data)
  const urlParameters = Object.keys(data).map(i => i + '=' + data[i]).join('&')
  return urlParameters
}

// 映射字段
export const mapField = (data, mapping, mergedData) => {
  let returnData
  if (typeof data['length'] !== 'undefined') {
    const freshDataList = []
    data.forEach((elem, index) => {
      let freshData = {}
      for (const column in elem) {
        const value = elem[column]
        if (typeof mapping[column] !== 'undefined') {
          freshData[mapping[column]] = value
        }
      }

      if (mergedData) {
        freshData = Object.assign({}, freshData, mergedData)
      }
      freshDataList.push(freshData)
    })
    returnData = freshDataList
  } else {
    const freshData = {}
    for (const column in data) {
      const value = data[column]
      if (typeof mapping[column] !== 'undefined') {
        freshData[mapping[column]] = value
      }
    }
    returnData = freshData
  }
  return returnData
}

// get extra params
export const getSiteParams = (fromStore) => {
  // const params = getQueryParams()
  // const storedSiteId = store.state.auth.site_id
  let args = {}
  if (fromStore) {
    args = {
      // 'site_id': storedSiteId
    }
  } else {
    // let siteId = 1
    // if (typeof params['site_id'] !== 'undefined') {
    //   siteId = params['site_id']
    // }
    args = {
      // 'site_id': siteId
    }
  }
  return args
}

export const getQueryParams = (url) => {
  if (!url) url = document.URL
	// get query string from url (optional) or window
  var queryString = url ? url.split('?')[1] : window.location.search.slice(1)
	// console.log(url, '=====>')
	// we'll store the parameters here
  var obj = {
    // site_id: 1
  }
	// if query string exists
  if (queryString) {
		// stuff after # is not part of query string, so get rid of it
    queryString = queryString.split('#')[0]
    console.log(queryString, 'queryString')
		// split our query string into its component parts
    var arr = queryString.split('&')
    for (var i = 0; i < arr.length; i++) {
			// separate the keys and the values
      var a = arr[i].split('=')
			// in case params look like: list[]=thing1&list[]=thing2
      var paramNum = 0
      var paramName = a[0].replace(/\[\d*\]/, function (v) {
        paramNum = v.slice(1, -1)
        return ''
      })
			// set parameter value (use 'true' if empty)
      var paramValue = typeof (a[1]) === 'undefined' ? true : a[1]
			// (optional) keep case consistent
      paramName = paramName.toLowerCase()
      paramValue = paramValue.toLowerCase()
			// if parameter name already exists
      if (obj[paramName]) {
				// convert value to array (if still string)
        if (typeof obj[paramName] === 'string') {
          obj[paramName] = [obj[paramName]]
        }
        console.log(paramNum, 'console.log(paramNum)', JSON.stringify(arr), paramName)
				// if no array index number specified...
        if (typeof paramNum === 'undefined') {
					// put the value on the end of the array
          obj[paramName].push(paramValue)
        } else {
					// if array index number specified...
					// put the value at that index number
          if (typeof obj[paramName] === 'object') {
            obj[paramName][paramNum] = paramValue
          }
        }
      } else {
				// if param name doesn't exist yet, set it
        // console.log(paramName, 'obj[paramName][paramNum]')
        obj[paramName] = paramValue
      }
    }
  }

  if (typeof obj['site_id'] === 'undefined') {
    // obj['site_id'] = 1
  }
  return obj
}
